package com.example.administrator.uitest.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.example.administrator.uitest.R;
import com.example.administrator.uitest.adapter.ListAdapter;
import com.example.administrator.uitest.intface.DoView;
import com.example.administrator.uitest.request.GetList_Request;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * Created by Maker on 2019/4/14.
 */

public class ListActivity extends Activity {
    private Button btn_user,btn_shoot;
    private ListView listView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_activity);
        initView();
        loadList(true);
    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(ListActivity.this,HomeActivity.class));
        finish();

    }

    private void loadList(final boolean flag) {
        //true 为用户排行 false 为游戏排行
        GetList_Request request = new GetList_Request(this,flag);
        request.conn(new DoView() {
            @Override
            public void setSuccessfulView(Object object) {
                Map<String,Object> all = (Map<String, Object>) object;
                List<Map<String,String>> data = (List<Map<String, String>>) all.get("data");
                Collections.sort(data, new Comparator<Map<String, String>>() {
                    @Override
                    public int compare(Map<String, String> stringStringMap, Map<String, String> t1) {
                        return Integer.valueOf(t1.get("shoot"))-Integer.valueOf(stringStringMap.get("shoot"));
                    }
                });
                listView.setAdapter(new ListAdapter(data,ListActivity.this, (Boolean) all.get("flag")));
            }

            @Override
            public void setFiledView(Object object) {

            }
        });

    }

    private void initView() {
        listView = findViewById(R.id.list);

        btn_user = findViewById(R.id.user);
        btn_shoot = findViewById(R.id.shoot);
        btn_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadList(true);
            }
        });
        btn_shoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadList(false);
            }
        });
    }
}
